package com.blackline.auto.Report;

import static org.junit.Assert.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.support.ui.Select;

import com.blackline.declareUI.UI;
import com.blackline.parameters.Parameters;
import com.blackline.parameters.TestData;
import com.blackline.auto.Report.*;
/*
 * test rail number -T139100
T139101
T139102
 */
public class Modify_Report {

	private WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();
	private static Logger logger=Logger.getLogger(Reports_smoketest.class);
	UI UI=new UI();
	Parameters parameters;
	TestData testdata = new TestData("TestData/ScheduleReport.xml");

	
	Schedule_UPS_report S1 =new Schedule_UPS_report();
	
	
	String group = testdata.getInput("G1");
	String team = testdata.getData("T1");
	
	String period = testdata.getData("P1");
	
	String entity = testdata.getData("E2");
	
	String report_name;
	
	String NameofReport="01";
	
	@Before
	public void setUp() throws Exception {

		
		FirefoxProfile profile=new FirefoxProfile();
		profile.setEnableNativeEvents(true);
		parameters=new Parameters("parameters.xml");
		driver=new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		UI.declareAllUI(driver);
		
			//S1.Schedule_UPS_test();
		
		//report_name=S1.NameofReport;
		
	}

	@After
	public void tearDown() throws Exception {
		
		driver.quit();

		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
		
	
	}

	
	
	@Test
	public void modify_Report() throws Exception  {
		
		

		logger.info("You've selected site: https://t2.blacklineondemand.com/Test2");
		UI.LogonPage.Login(driver, "https://t2.blacklineondemand.com/Test2", parameters.getLogonID("admin"), parameters.getPassword("admin"));
		//Thread.sleep(3000);
		  UI.NavigationMenu.REPORTS.click();
		  UI.Reports.Enhanced_Reporting_link.click();
		  Thread.sleep(20000);
		
		
		  
		  schedule_Report();
		
		 UI.Reporting.My_scheduled_Report_nodeInLeftTree.click();
		  Thread.sleep(20000);
		  // reaching till scheduled report 
		  UI.Reporting.Name_text_field.clear();
		  UI.Reporting.Name_text_field.sendKeys(NameofReport);
		  
		  logger.info("report_name");
		  UI.Reporting.Refresh_button.click();
		  
		  System.out.println("selected");
		  
		  Thread.sleep(3000);
		  
		  UI.Reporting.Daily_linkOfSearchedScheduledReport.click();
		  Thread.sleep(2000);
		
		  
		  
		 
		  
		  new Select(driver.findElement(By.cssSelector("#ctl00_ctl00_contentBody_cphMain_promptWindow_promptModal_accountGroupDropdown > #groupsPrompt"))).selectByVisibleText(group);
		 
		  
		  
		  
		  new Select(driver.findElement(By.id("ctl00_ctl00_contentBody_cphMain_promptWindow_promptModal_periods_periodsDropDown"))).selectByVisibleText(period);
		  
		  
		
		 
		//  System.out.println("clickking the image button ");
		  
		  UI.SchedulePrompts.Schedule_entityview_link.click();
		  
		  Thread.sleep(2000);
		  
		 UI.SchedulePrompts.Schedule_Entity_DropdownListinput.clear();
		 
		 Thread.sleep(2000);
		 UI.SchedulePrompts.Schedule_Entity_DropdownListinput.sendKeys(entity);
		 
		 Thread.sleep(2000);
		 
		
		 UI.SchedulePrompts.Schedule_report_control.click();
		 Thread.sleep(2000);
		 
		 UI.SchedulePrompts.Schedule_Team_DropdownListinput.clear();
	 
		 Thread.sleep(2000);

		 UI.SchedulePrompts.Schedule_Team_DropdownListinput.sendKeys(team);
		
		 
		 
		 
		 
		 
		
		 Thread.sleep(2000);
		 
		
		 
		 UI.SchedulePrompts.Save_Report_button.click();
		 
		 
		 Thread.sleep(20000);
		 String runRes=UI.Alert_inReporting.Message_Text.getText();
		 
			
			if(runRes.contains("has been saved"))
			  {
				  logger.info("[Pass!] "+runRes);
				  UI.Alert_inReporting.OK_button.click();
			  }
			
			  else{
				  logger.error("[Fail!] "+runRes);
				  verificationErrors.append("[Fail!] "+runRes);
				  fail("Schedule failed: "+runRes);
			  }
		 
	
		 
		/**
		 * 
		 * Verification if user is able to save the changes 
		 *  
		 * 
		 */
			UI.Reporting.My_scheduled_Report_nodeInLeftTree.click();
			  Thread.sleep(20000);
			  // reaching till scheduled report 
			  UI.Reporting.Name_text_field.clear();
			  UI.Reporting.Name_text_field.sendKeys(NameofReport);
			 
			  /*
			   * group
			   * */
			
	
			  
			  logger.info("UPS02");
			  UI.Reporting.Refresh_button.click();
			  
			  System.out.println("selected");
			  
			  Thread.sleep(3000);
			  UI.Reporting.Daily_linkOfSearchedScheduledReport.click();
			  Thread.sleep(20000);
			  
		  String groupv= new Select(driver.findElement(By.cssSelector("#ctl00_ctl00_contentBody_cphMain_promptWindow_promptModal_accountGroupDropdown > #groupsPrompt"))).getFirstSelectedOption().getText();
			 
		  System.out.println(groupv);
		  
		  if(groupv.equals(group))
		  {
			  logger.info("[Pass!] "+"group name matched ");
			 
		  }
		
		  else{
			  logger.error("[Fail!] "+"group name matched ");
			  verificationErrors.append("[Fail!] "+"group name matched ");
			  fail("Schedule failed: "+"group name matched ");
		  }
		  
		  
		  
		
		  
		  
		  
		  
		  /*
		   * period
		   * */
		  
		 String periodv =new Select(driver.findElement(By.id("ctl00_ctl00_contentBody_cphMain_promptWindow_promptModal_periods_periodsDropDown"))).getFirstSelectedOption().getText();
		 
		  System.out.println(periodv);
		  
		  if(periodv.equals(period))
		  {
			  logger.info("[Pass!] "+"period name matched ");
			 
		  }
		
		  else{
			  logger.error("[Fail!] "+"period name  ");
			  verificationErrors.append("[Fail!] "+"period name  ");
			  fail("Schedule failed: "+"period name  ");
		  }
		 
		  
		  /*
		   * team
		   * */
		  
		String teamv=  UI.SchedulePrompts.Schedule_Team_DropdownListinput.getAttribute("value");
		
		if(teamv.equals(team))
		  {
			  logger.info("[Pass!] "+"team name  ");
			 
		  }
		
		  else{
			  logger.error("[Fail!] "+"team name  ");
			  verificationErrors.append("[Fail!] "+"team name  ");
			  fail("Schedule failed: "+"team name  ");
		  }
		
		
		
		//entity
		
		 String entityv=UI.SchedulePrompts.Schedule_Entity_DropdownListinput.getAttribute("value");
		 
		 
		 if( entityv.equals(entity))
		  {
			  logger.info("[Pass!] "+" entity");
		
		  }
		
		  else{
			  logger.error("[Fail!] "+"entity name  ");
			  verificationErrors.append("[Fail!] "+" entity" );
			  fail("Schedule failed: "+"entity name  ");
		  }
		 
		 UI.SchedulePrompts.Cancel_button.click();
		  
		 
		  	}
	/*
	 * to schedule single report 
	 * 
	 */
	
	public void schedule_Report() throws Exception  {
		
	
		Date now=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
		Calendar cal=Calendar.getInstance();
		cal.setTime(now);
		cal.add(Calendar.DAY_OF_MONTH, 1);
		String startDate=sdf.format(cal.getTime());
		logger.info(startDate);

		// current time  to append it in name
		SimpleDateFormat sdf1=new SimpleDateFormat("hh:mm");
		Calendar cal1=Calendar.getInstance();
		cal1.setTime(now);
		cal1.add(Calendar.DAY_OF_MONTH, 1);
		String startTime=sdf1.format(cal1.getTime());
		logger.info(startTime);
		
		
		SimpleDateFormat sdf2=new SimpleDateFormat("yyyy");
		String years=sdf2.format(cal.getTime());
		String endDate="12/31/"+years;
		logger.info(endDate);
		
		
		
		  //ups Reports 
		  
		  UI.Reporting.AddIcon_UPSReports.click();
		  UI.Reporting.UPS_nodeInLeftTree.click();
		  Thread.sleep(20000);
		  
		  UI.Reporting.Name_text_field.clear();
	 		UI.Reporting.Name_text_field.sendKeys(NameofReport);
	 		UI.Reporting.Refresh_button.click();
	 		
	  String schedulename = UI.Reporting.Schedule_Name_SearchedResult.getText();
	 logger.info(schedulename);
	  
	 
	 
	 assertNotNull(UI.Reporting.There_are_no_records_text);
	 // enter the scheduling 
	 UI.Reporting.Schedule_linkOfSearchedResult.click();
	 
	 
	 UI.SchedulePrompts.Schedule_Name_Text.clear();
	 UI.SchedulePrompts.Schedule_Name_Text.sendKeys("UPS"+NameofReport+startDate+startTime);
	 // edit of start date
	 UI.SchedulePrompts.Schedule_Start_Date_Text.clear();
	 UI.SchedulePrompts.Schedule_Start_Date_Text.sendKeys(startDate);
	 //editing end date 
	 UI.SchedulePrompts.Schedule_End_Date_Text.clear();
	 UI.SchedulePrompts.Schedule_End_Date_Text.sendKeys(endDate);
	 
	 UI.SchedulePrompts.Schedule_Report_button.click();
	 
	 Thread.sleep(20000);
	// Thread.sleep(25000);
	 // missed the box and click on ohk .....

	 String runRes=UI.Alert_inReporting.Message_Text.getText();
	 
	
		if(runRes.contains("has been created"))
		  {
			  logger.info("[Pass!] "+runRes);
			  UI.Alert_inReporting.OK_button.click();
		  }
		
		  else{
			  logger.error("[Fail!] "+runRes);
			  verificationErrors.append("[Fail!] "+runRes);
			  fail("Schedule failed: "+runRes);
		  }
	 
		
		
		
		NameofReport="UPS"+NameofReport+startDate+startTime;
		
		
		
		  
		
		
	}
	
	
	
		
		
		
		
		
		
		
		
		
		
		
	}
		
	
	


